﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chapter16.Code01.KarliCards_GUI.ViewModel
{
    using CardLib;
    using System.ComponentModel;
    using System.Collections.ObjectModel;
    using System.Windows.Input;
    using System.IO;
    using System.Xml.Serialization;

    [Serializable]
    public class GameOptions : INotifyPropertyChanged
    {
        public static RoutedCommand OptionsCommand =
            new RoutedCommand(
                "Show Options",
                typeof(GameOptions),
                new InputGestureCollection(new List<InputGesture> { new KeyGesture(Key.O, ModifierKeys.Control) }));

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private bool playAgainstComputer = true;
        public bool PlayAgainstComputer
        {
            get { return playAgainstComputer; }
            set { playAgainstComputer = value; OnPropertyChanged("PlayAgainstComputer"); }
        }

        private int numberOfPlayers = 2;
        public int NumberOfPlayers
        {
            get { return numberOfPlayers; }
            set { numberOfPlayers = value; OnPropertyChanged("NumberOfplayers"); }
        }

        private int minutesBeforeLoss = 10;
        public int MinutesBeforeLoss
        {
            get { return minutesBeforeLoss; }
            set { minutesBeforeLoss = value; OnPropertyChanged("MinutedBeforeLoss"); }
        }

        private ComputerSkillLevel computerSkill = ComputerSkillLevel.Dumb;
        public ComputerSkillLevel ComputerSkill
        {
            get { return computerSkill; }
            set { computerSkill = value; OnPropertyChanged("ComputerSkill"); }
        }

        private ObservableCollection<string> playerNames = new ObservableCollection<string>();
        public ObservableCollection<string> PlayerNames
        {
            get { return playerNames; }
            set { playerNames = value; OnPropertyChanged("PlayerNames"); }
        }

        public List<string> SelectedPlayers { get; set; }

        private static string gameOptionsFile = "GameOptions.xml";

        public GameOptions()
        {
            SelectedPlayers = new List<string>();
        }

        public void AddPlayer(string playerName)
        {
            if (playerNames.Contains(playerName)) return;
            playerNames.Add(playerName);
            OnPropertyChanged("PlayerNames");
        }

        public void Save()
        {
            using (var stream = File.Open(gameOptionsFile, FileMode.Create))
            {
                var serializer = new XmlSerializer(typeof(GameOptions));
                serializer.Serialize(stream, this);
            }
        }

        public static GameOptions Create()
        {
            if (File.Exists(gameOptionsFile))
            {
                using (var stream = File.OpenRead(gameOptionsFile))
                {
                    var serializer = new XmlSerializer(typeof(GameOptions));
                    return serializer.Deserialize(stream) as GameOptions;
                }
            }
            else
                return new GameOptions();
        }
    }
}
